<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>

	<head>
		<title>业务处理-普通入库-新增普通入库</title>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">
	</head>

	<body>
		<div id="mainContent">
			<div id="mainContent_content">
				<div class="page_header">
					<a href="<%=basePath%>pickingReturnContrlooer/selectByWhere.do">普通入库</a>
					<i class="icon-double-right"></i>
					<a>新增普通入库</a>
				</div>

				<div class="btn rightBtn">
					<input type="button" value="新增物品" onclick="toAddPurchaseRequestItem();" class="btn btn1"></input>
					<input type="button" value="保存" onclick="saveAddPurchaseRequest();" class="btn btn2"></input>
					<!-- <button class="btn btn1" style="margin-right: 10px;" id="submitOrder" >Excel导入</button> -->
					<input type="button" value="返回" onclick="location.href='<%=basePath%>generalStorage/selectByWhere.do'" class="btn btn1"></input>
					<!-- <form id="submitOrderForm" method="post" enctype="multipart/form-data">
	                	<input type="file" style="display: none;" id="submitOrderFile" name="submitOrder_files"
	                	accept=".csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
	            	</form> -->
				</div>
				<form id="generalStorageForm" class="form">
					<div id="requisitionForm" class="box">
						<p>
							<label>入库单号</label>
							<input type="text" readonly class="form-control" placeholder="自动生成"></input>

							<label>入库人</label>
							<input type="text" readonly class="form-control" value="${userName }"></input> <input type="hidden" value="${userID }" name="purchaserid"></input>
							<input type="hidden" value="${userID }" name="createpersonid"></input> <input type="hidden" value="${userID }" name="lastmodifiedid"></input>

							<label> 类型</label>
							<select name="materialMaterial2" class="form-control select" id="materialMaterial2" onchange="gradeChange()">
								<option value="IMPA物料">IMPA物料</option>
								<option value="普通物料">普通物料</option>
							</select>

							<label> 状态</label>
							<select name="status" class="form-control select">
								<option value="0">未提交</option>
								<option value="1">已提交</option>
							</select>

						</p>
						<p>
							<label> 仓库</label>
							<select name="warehouseid" class="form-control select">
								<c:forEach items="${warehouseList }" var="warehouse">
									<option value="${warehouse.warehousemanagementid }">${warehouse.warehousename }</option>
								</c:forEach>
							</select>

							<label>供应商</label>
							<select name="supplierID" onchange="gradeChange()" class="form-control select">
								<c:forEach items="${supplierList }" var="supplier">
									<option value="${supplier.supplierid }">${supplier.chinesename }</option>
								</c:forEach>
							</select>

							<label> 说明</label>
							<input type="text" class="form-control" name="storageinstructions"></input>

						</p>

						<p>
							<label>备注</label>
							<input type="text" class="form-control doubleWidth" name="remarks"></input>
						</p>
					</div>
					<div class="tableBox box">
						<table class="table table-bordered table_text_center">
							<thead>
								<tr>
									<th>代码</th>
									<th>物品名称</th>
									<th>物品条码</th>
									<th>品牌</th>
									<th>库存</th>
									<th>过期时间</th>
									<th>采购数量</th>
									<th>规格</th>
									<th>数量</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody id="purchaseRequestItemTbody">
							</tbody>
						</table>
						<!-- 				<div class="btn">
					<input type="button" value="编辑"></input>
					<input type="button" value="导出模板"></input>
					<input type="button" value="导入物品"></input>
				</div> -->
					</div>
				</form>
			</div>
			<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
			<jsp:include page="/jsp/storage/normalStorage/addPurchaseRequestItem.jsp"></jsp:include>
		</div>
		<script src="<%=basePath%>js/jquery-3.2.1.min.js"></script>
		<script src="<%=basePath%>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/bootstrap-table.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/locale/bootstrap-table-zh-CN.min.js"></script>
		<script src="<%=basePath%>My97DatePickers/WdatePicker.js"></script>
		<script src="<%=basePath%>js/public.js"></script>
		<script type="text/javascript">
			function gradeChange() {
				$("#add_generalStorage #materialMaterial").val($("#materialMaterial2 option:selected").val());
				$('#purchaseRequestItemTbody').empty();
			}

			$(function() {
				// 任何需要执行的js特效 
				$("#add_generalStorage #materialMaterial").val($("#materialMaterial2 option:eq(0)").val());
			});

			function addToHasAddTable(item) {
				var html = "<tr>";
				html += "<td>" + item.itemcode + "</td>";
				html += "<td>" + item.chinesename + "</td>";
				html += "<td>" + item.articlebarcode + "</td>";
				html += "<td>" + item.brand + "</td>";
				html += "<td>" + item.currentstock + "</td>";
				html += "<td><input type='text' name='expiryDate' readonly onclick='WdatePicker({skin:\"whyGreen\",dateFmt:\"yyyy-MM-dd\"})' class='form-control'/></td>";

				html += "<td><input name='itemNumber2' maxlength='8'  oninput='calculations(this)' type='text' class='form-control'/></td>";
				html += "<td><select oninput='calculations2(this)' class='form-control select'>" +
					"<option value='" + item.salesspecifications + "'>" + item.salesspecifications + "" + item.salesunit + "</option>" +
					"<option value='" + item.purchasespecifications + "'>" + item.purchasespecifications + "" + item.salesunit + "/" + item.purchasingunit + "</option>" +
					"</select></td>";

				html += "<td><input type='text' readonly name='itemNumber' onkeyup='limitInputNumber(this);' onchange='limitInputNumber(this);' class='form-control'/></td>";
				html += "<td><a onclick='deleteItem(this);' class='btn btn-xs btn_danger'>删除</a>";
				html += "<input type='hidden' name='itemIds' value='" + item.inteminformationid + "' /></td>";
				html += "</tr>";
				$("#purchaseRequestItemTbody").append(html);
			}

			function deleteItem(obj) {
				$(obj).parent().parent().remove();
			}
			/* 输入采购数量自动计算数量 */
			function calculations(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).val().replace(/\D/g, '')) * ($(obj).parent().next().find("option:selected").val()));
			}
			/* 选择规格下拉框在自动计算数量 */
			function calculations2(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).parent().parent().find("input[name='itemNumber2']").val().replace(/\D/g, '')) * ($(obj).find("option:selected").val()));
			}

			function validateForm() {
				/* 检查是否新增了物品 */
				var length = $("#purchaseRequestItemTbody [name='itemIds']").length;
				if(!length > 0) {
					Prompt.alert("提示", "物品种类数量必须大于0！")
					return false;
				}
				/* 检查数量是否大于0 */
				var arr = $("#purchaseRequestItemTbody [name='itemNumber']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					if(num.trim() == '' || num <= 0) {
						Prompt.alert("提示", "物品采购数量必须大于0！")
						return false;
					}
				}
				/* 检查日期是否填写 */
				var arr = $("#purchaseRequestItemTbody [name='expiryDate']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					if(num.trim() == '') {
						Prompt.alert("提示", "必须填写日期！")
						return false;
					}
				}
				return true;
			}

			function saveAddPurchaseRequest() {
				if(!validateForm()) return;
				$.ajax({
					url: "<%=basePath%>generalStorage/add.do",
					type: "post",
					async: false,
					dataType: "json",
					data: $("#generalStorageForm").serialize(),
					success: function(data) {
						if(data.result == "success") {
							Prompt.alert("提示", "普通入库保存成功！", function() {
								location.href = "<%=basePath%>generalStorage/selectByWhere.do";
							});
						} else {
							Prompt.alert("提示", "普通入库保存失败！");
						}
					},
					error: function() {
						Prompt.alert("提示", "普通入库保存失败！");
					}
				});
			}
			
			function inputitems(){
				
			}
			
			//========================================================================
			/* 采购入库导入Excel */
			$("#submitOrder").unbind();
			$("#submitOrder").on("click",function() {
				$("#submitOrderFile").click();
				$("#submitOrderFile").unbind();
				$("#submitOrderFile").on("change",function() {
					if($(this).val()){
						ajaxSubmitMissExamFile();
					}else{
						createPrompt("请选择Excel文件！");
					}
				});
				
			});
			//利用ajax多文件提交
			<%-- function ajaxSubmitMissExamFile(){
				//显示进度条
				$("#parent_son").css("display","block");
				$("#parent").css("display","block");
				var height=$(window).height();
				$("#parent").css("left",height-250);
				
				var formData = new FormData($("#missExamForm")[0]);
			    $.ajax({
			         url: "<%=basePath %>manage/register/uploadMissExamFile",
			         type: "POST",
			         data: formData,
			         async: true,
			         contentType: false,
			         processData: false,
			         success: function (data) {
			        	 $("#parent_son").css("display","none");
			        	 $("#parent").css("display","none");//隐藏进度条
			        	 
			        	 var funcReload=function(){
			            	 location.reload();
			        	 }
			        	 createAlertPrompt(data,funcReload);
			        	 
			        	 $("#prompt_alert").css("max-width","400px");
			        	 $("#tisiP").css("max-height","150px");
			        	 $("#tisiP").css("overflow-y","auto");
			        	 $("#tisiP").css("overflow-x","hidden");
			        	 $("#tisiP").css("text-align","left");
			        	 setCenter("prompt_alert");
			         },
			         error: function(){
			        	 location.reload();
			         },
			         xhr: function(){
			             var xhr = $.ajaxSettings.xhr();
			             if(onprogress && xhr.upload) {
			                 xhr.upload.addEventListener("progress" , onprogress, false);
			                 return xhr;
			             }
			         }
			    }); 
			}--%>
		</script>
	</body>

</html>